package org.softmed.rest.editor;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;


class CloseDialog extends WindowAdapter {
	
	private Editor editor;

	public CloseDialog(Editor editor){
		this.editor = editor;
	}
	
	@Override
	public void windowClosing(WindowEvent we) {
		if (editor.DIRTY) {
			int duh = JOptionPane
					.showConfirmDialog(
							editor,
							"You have unsaved changes on this scene.\n"
									+ "Would you like to cancel loading so that you can save this scene ???\n"
									+ "If you click yes, you will cancel loading a file, and you will have\n the option of saving this scene."
									+ "\nIf you click no you will lose all changes applied to this scene.",
							"test2", JOptionPane.YES_NO_OPTION,
							JOptionPane.QUESTION_MESSAGE);
			if (duh == JOptionPane.YES_OPTION)
				return;

		}
		System.exit(0);

	}
}

public abstract class Editor extends JFrame {

	public boolean DIRTY = false;

	JPanel left = new JPanel();

	JPanel right = new JPanel();

	JPanel center = new JPanel();

	JPanel bottom = new JPanel();

	JToolBar toolbar = new JToolBar(null, JToolBar.HORIZONTAL);

	private Container container;

	static String fileName;

	public void setDirty(boolean state) {
		refreshTitle(Editor.fileName, state);
	}

	@Override
	public void setTitle(String msg) {
		// fileName = msg;
		refreshTitle(msg, false);
	}

	protected void refreshTitle(String msg, boolean dirty) {
		if (DIRTY != dirty || !msg.equals(Editor.fileName)) {
			Editor.fileName = msg;
			DIRTY = dirty;
			super.setTitle((DIRTY ? "*" : "") + fileName
					+ " - REST Config Editor - Early Alpha V0.01");
		}
	}

	public Editor() {

		setTitle("untitled");

		container = this.getContentPane();
		container.setLayout(new BorderLayout());

		center.setLayout(new BoxLayout(center, BoxLayout.PAGE_AXIS));

		// this.s

		container.add(toolbar, BorderLayout.NORTH);
		container.add(center, BorderLayout.CENTER);
		container.add(bottom, BorderLayout.SOUTH);
		container.add(left, BorderLayout.WEST);
		container.add(right, BorderLayout.EAST);

		center.setPreferredSize(new Dimension(550, 650));
		center.setMinimumSize(new Dimension(550, 650));
		center.setMaximumSize(new Dimension(550, 650));
		
		
		right.setPreferredSize(new Dimension(300, 650));
		right.setMinimumSize(new Dimension(300, 650));
		right.setMaximumSize(new Dimension(300, 650));
		
		left.setPreferredSize(new Dimension(150, 650));
		left.setMinimumSize(new Dimension(150, 650));
		left.setMaximumSize(new Dimension(150, 650));


		/*
		 * left.setPreferredSize(new Dimension(260, 700));
		 * left.setMaximumSize(new Dimension(260, 700)); left.setSize(new
		 * Dimension(260, 700)); //
		 */

		this.setPreferredSize(new Dimension(1024, 768));
		this.setMinimumSize(new Dimension(1024, 768));
		this.setMaximumSize(new Dimension(1024, 768));

		setup();
		
		this.pack();
		this.setVisible(true);
		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		// setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

		addWindowListener(new CloseDialog(this));

	}

	protected abstract void setup();

	public JToolBar getToolbar() {
		return toolbar;
	}

}
